using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

using SLIITPro.Core;
using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

namespace SLIITPro.Data.Providers
{
    public class UserRoleProvider : RoleProvider
    {
        private string connStringName;
        private string applicationName;
        private MembershipPasswordFormat passwordFormat;
        private UserExtension _userExtension;
        private Datamanager _datamanager;
        private Users _users;
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            if (config["connectionStringName"] == null)
            {
                config["connectionStringName"] = "SLIITProConnectionString";
            }
            connStringName = config["connectionStringName"];
            config.Remove("connectionStringName");

            if (config["applicationName"] == null)
            {
                config["applicationName"] = "SLIITPro";
            }
            applicationName = config["applicationName"];
            config.Remove("applicationName");

        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            var users = new List<string>();
            _datamanager = new Datamanager();
            var query = SLIITPro.Data.Properties.Resources.RProviderGetRolesForUserQuery;
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@1", SqlDbType.NVarChar) { Value = username };
            var table = _datamanager.ExecuteQuery(query, "Users", parameters);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var tb = table.Rows[i].ItemArray[0].ToString();
                    users.Add(tb);
                }
            }

            return users.ToArray();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var users = new List<string>();
            _datamanager = new Datamanager();
            var query = SLIITPro.Data.Properties.Resources.RProviderGetUserInRoleQuery;
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@1", SqlDbType.NVarChar) { Value = roleName };
            var table = _datamanager.ExecuteQuery(query, "Users", parameters);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var tb = table.Rows[i].ItemArray[0].ToString();
                    users.Add(tb);
                } 
            }
            return users.ToArray();
        }
        //        select distinct UserType from Users
        public override string[] GetAllRoles()
        {
            var users = new List<string>();
            _datamanager = new Datamanager();
            var query = SLIITPro.Data.Properties.Resources.RProviderGetAllUserQuery;
            var table = _datamanager.ExecuteQuery(query, "Users", null);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var tb = table.Rows[i].ItemArray[0].ToString();
                    users.Add(tb);
                } 
            }
            return users.ToArray();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { return applicationName; }
            set { throw new NotImplementedException(); }
        }
    }
}